/* Copyright 2017, 2018, Gabriel Czernikier
 *
 * This file is part of Notehaus.
 * Notehaus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Notehaus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with Notehaus.  If not, see <http://www.gnu.org/licenses/>.
 */
#define _BSD_SOURCE
#include <endian.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "composer.h"
#include "lgwseq.h"
#include "types.h"

int up_bounds_ary[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int alignment_ary[] = {1, 1, 4, 4, 4, 4, 4, 1, 1};
int majors_size_ary[NH_UP_BOUNDS_SZ];   // late-time (more recent) naming style coding changed from "majors" to some name particles/abbreviations standing for "equivalence class representative" in order to refering to the same thing.
unsigned char *buffer[NH_UP_BOUNDS_SZ];
size_t buf_len[NH_UP_BOUNDS_SZ];
size_t buf_offset[NH_UP_BOUNDS_SZ];

void bounds_move(int idx, unsigned int amount) {
  for(;idx<NH_UP_BOUNDS_SZ; idx++)
    up_bounds_ary[idx]+=amount;
}

void
align_bounds() {
  // Adjusts bounds
  for(int i=1; i!=NH_UP_BOUNDS_SZ; i++) {
    if(majors_size_ary[i]!=0)
      bounds_move(i-1, /*padding*/(alignment_ary[i]-up_bounds_ary[i-1]%alignment_ary[i])%alignment_ary[i]);
  }
  // Adjustment in accordance with a clause in the header_item specification.
  bounds_move(NH_DATA_UBOUND-1, (sizeof(uint32_t) - (up_bounds_ary[NH_DATA_UBOUND-1]-up_bounds_ary[NH_DATA_LBOUND-1]%sizeof(uint32_t)))%sizeof(uint32_t));
  // Fills the buffers up to their adjusted section's boundaries
  //static const char FILLPATTERN[] = "\0\0\0\0\0\0"; // Also count on an implicit \0 automatically stored as of C language's approach to string representation.
  /* The padding can be done as an appended job when the end format is being written from buffers to the output file.
     Moreover, doing the padding right now, when there are still buffers without data (and that is always the case for NH_HEADER_IDX and NH_MAP_LIST_IDX buffers) should not be the purpose of what is to pad-align bounds between sections represented by those buffers. This is as unnecessary as inefficient, and unnecessary stands for ineffective.
  for(int i=1; i!=NH_UP_BOUNDS_SZ; i++) {
    int buf_len_wout_pad= buf_len[i];  // avoid side effect of LEN Macro's parameter being functionaly dependent on buf_len[i], and unintentionaly modified following the macro modifying that former variable that also goes as a parameter.
    BUFFER_WRITE(buffer[i],buf_len[i],FILLPATTERN,buf_offset[i],up_bounds_ary[i]-(up_bounds_ary[i-1]+buf_len_wout_pad))
  }
  */
}


